package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.*;
import com.goods.business.service.InStockServices;
import com.goods.common.model.business.*;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ：MJS
 * @date ：Created in 2021/6/8  22:54
 * @description：TODO
 */
@Service
public class InStockServicesImpl implements InStockServices {
    @Autowired(required = false)
    private InStockMapper inStockMapper;
    @Autowired(required = false)
    private SupplierMapper supplierMapper;
    @Autowired(required = false)
    private InStockInfoMapper inStockInfoMapper;
    @Autowired(required = false)
    private ProductMapper productMapper;
    @Autowired(required = false)
    private ProductStockMapper productStockMapper;
    //加载入库表格数据  模糊查询
    @Override
    public PageVO<InStockVO> findInStockList(Integer pageNum, Integer pageSize, InStockVO inStockVO) {
        //已经拥有的  进货的表
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(InStock.class);
        //类型
        Integer type = inStockVO.getType();
        //单号
        String inNum = inStockVO.getInNum();
        //状态
        Integer status = inStockVO.getStatus();
        //开始时间
        Date startTime = inStockVO.getStartTime();
        //结束时间
        Date endTime = inStockVO.getEndTime();

        Example.Criteria criteria = o.createCriteria();

        if(type!=null&&!"".equals(type)){
            criteria.andEqualTo("type",type);
        }
        if(inNum!=null&&!"".equals(inNum)){
            criteria.andEqualTo("inNum",inNum);
        }
        if(status!=null&&!"".equals(status)){
            criteria.andEqualTo("status",status);
        }

        if(startTime!=null&&!"".equals(startTime)){
            criteria.andGreaterThan("createTime",startTime);
        }
        if(endTime!=null&&!"".equals(endTime)){
            criteria.andLessThan("createTime",endTime);
        }
        List<InStock> inStockList = inStockMapper.selectByExample(o);
        List<InStockVO> inStockVOS = inStockList.stream().map(inStock -> {
            InStockVO inStockVO1 = new InStockVO();
            BeanUtils.copyProperties(inStock, inStockVO1);
            Long supplierId = inStockVO1.getSupplierId();
            //建立物资来源表
            Supplier supplier1 = new Supplier();
            supplier1.setId(supplierId);
            Supplier supplier = supplierMapper.selectOne(supplier1);
            String phone = supplier.getPhone();
           String name = supplier.getName();
            inStockVO1.setPhone(phone);
            inStockVO1.setSupplierName(name);
            return inStockVO1;
        }).collect(Collectors.toList());
        PageInfo<InStock>info=new PageInfo(inStockList);
        return new PageVO<>(info.getTotal(),inStockVOS);
    }
    //查询入库明细
    @Override
    public InStockVO detail(Long id, Integer pageNum) {

        InStockVO inStockVO = new InStockVO();
        InStock inStock = new InStock();
        inStock.setId(id);
        InStock inStock1 = inStockMapper.selectOne(inStock);
        Integer status = inStock1.getStatus();
        //查询inStock的inNum
        String inNum1 = inStock1.getInNum();
        //查询inStock的supplierId
        Long supplierId = inStock1.getSupplierId();

        //建立inStockInfo表
        InStockInfo inStockInfo = new InStockInfo();
        inStockInfo.setInNum(inNum1);
        //查询inStockInfo表
        List<InStockInfo> inStockInfoList = inStockInfoMapper.select(inStockInfo);
        List<Object> productList = new ArrayList<>();
        for (InStockInfo stockInfo : inStockInfoList) {
            //查询inStockInfo的pNum
            String pNum = stockInfo.getPNum();
            //查询数量
            Integer productNumber = stockInfo.getProductNumber();

            //建立product表
            Product product = new Product();
            product.setPNum(pNum);
            //查询product表
            Product product1 = productMapper.selectOne(product);
            //实体类转换vo
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product1,productVO);
            //vo添加数量
            productVO.setCount(productNumber);

            productList.add(productVO);
        }
        inStockVO.setProducts(productList);

        //建立supplier表
        Supplier supplier1 = new Supplier();
        supplier1.setId(supplierId);
        //查询supplier表
        Supplier supplier = supplierMapper.selectOne(supplier1);
        //省区市
        String address = supplier.getAddress();
        inStockVO.setAddress(address);
        //详细地址
        String name = supplier.getName();
        inStockVO.setName(name);
        //手机号
        String phone = supplier.getPhone();
        inStockVO.setPhone(phone);
        //联系人
        String contact = supplier.getContact();
        inStockVO.setContact(contact);
        inStockVO.setStatus(status);

       return inStockVO;
    }
    //移入回收站
    @Override
    public void remove(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        InStock inStock1 = inStockMapper.selectOne(inStock);
        inStock1.setStatus(InStock.DELETE);
        inStock1.setModified(new Date());
        Example o = new Example(inStock.getClass());
        o.createCriteria().andEqualTo("id",id);
        inStockMapper.updateByExample(inStock1,o);
    }
    //从回收站恢复
    @Override
    public void back(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        InStock inStock1 = inStockMapper.selectOne(inStock);
        inStock1.setStatus(InStock.NORMAL);
        inStock1.setModified(new Date());
        Example o = new Example(InStock.class);
        o.createCriteria().andEqualTo("id",id);
        inStockMapper.updateByExample(inStock1,o);
    }
    //删除
    @Override
    public void delete(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        InStock inStock1 = inStockMapper.selectOne(inStock);
        if(inStock1.getStatus()==1){
            inStockMapper.delete(inStock);
        }
    }
    //添加入库单
    @Override
    public void addStock(InStockVO inStockVO) {
        Long supplierId = inStockVO.getSupplierId();
        if(supplierId==null){
            //新建supplier表  将inStockVO数据传入supplier
            Supplier supplier = new Supplier();
            BeanUtils.copyProperties(inStockVO,supplier);
            supplier.setCreateTime(new Date());
            supplier.setModifiedTime(new Date());
            supplierMapper.insert(supplier);
            //获取刚刚添加的信息的主键ID
            supplierId = supplier.getId();
        }
        //设置uuid
        String inNum = UUID.randomUUID().toString();
        //获取products
        List<Object> products = inStockVO.getProducts();

        for (Object product : products) {
            //新建InStockInfo表
            InStockInfo inStockInfo = new InStockInfo();
            Map product1 = (Map) product;
            Object productId = product1.get("productId");
            Integer productNumber = (Integer)product1.get("productNumber");
            Example o = new Example(Product.class);
            o.createCriteria().andEqualTo("id",productId);
            Product product2 = productMapper.selectOneByExample(o);
            String pNum = product2.getPNum();
            inStockInfo.setPNum(pNum);
            inStockInfo.setProductNumber(productNumber);
            inStockInfo.setInNum(inNum);
            inStockInfo.setCreateTime(new Date());
            inStockInfo.setModifiedTime(new Date());
            inStockInfoMapper.insert(inStockInfo);
        }

        //获取商品总类数量
        int productNum = products.size();

        //建实体类
        InStock inStock = new InStock();
        //vo传实体类
        BeanUtils.copyProperties(inStockVO,inStock);
        //创建时间
        inStock.setCreateTime(new Date());
        // 修改时间
        inStock.setModified(new Date());
        //设置supplier_id
        inStock.setSupplierId(supplierId);
        //设置商品总类的数量
        inStock.setProductNumber(productNum);
        //设置UUID与inStockInfo的UUID一样
        inStock.setInNum(inNum);
        //设置  审核状态 默认 待审核
        inStock.setStatus(InStock.AUDIT);
        //设置操作人员
        inStock.setOperator("admin");

        inStockMapper.insert(inStock);
    }
    //入库单审核
    @Override
    public void publish(Long id) {
        //建inStock表
        InStock inStock = new InStock();
        inStock.setId(id);
        //查询inStock
        InStock inStock1 = inStockMapper.selectOne(inStock);
        //改变inStock的状态
        inStock1.setStatus(InStock.NORMAL);
        //修改日期
        inStock1.setModified(new Date());
        //获取入库单编号
        String inNum = inStock1.getInNum();
        //建InStockInfo表
        InStockInfo inStockInfo = new InStockInfo();
        inStockInfo.setInNum(inNum);
        //查询订单详情表
        List<InStockInfo> inStockInfoList = inStockInfoMapper.select(inStockInfo);
        //循环遍历每一个商品
        for (InStockInfo stockInfo : inStockInfoList) {
            //获取商品编号
            String pNum = stockInfo.getPNum();
            //获取商品数量
            Integer productNumber = stockInfo.getProductNumber();

            ProductStock productStock = new ProductStock();
            productStock.setPNum(pNum);
            //查询库存中的商品
            productStock = productStockMapper.selectOne(productStock);

            productStock.setStock(productStock.getStock()+productNumber);
            Example e = new Example(productStock.getClass());
            e.createCriteria().andEqualTo("pNum",pNum);
            productStockMapper.updateByExample(productStock,e);

        }

        Example o = new Example(inStock.getClass());
        o.createCriteria().andEqualTo("id",id);
        inStockMapper.updateByExample(inStock1,o);

    }
}
